package com.skyun.watchmanager.wear;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.support.wearable.view.BoxInsetLayout;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import me.skyun.androidarch.common.Utils;

public class MainActivity extends WearableActivity {

    private static final SimpleDateFormat AMBIENT_DATE_FORMAT =
            new SimpleDateFormat("HH:mm", Locale.US);

    private BoxInsetLayout mContainerView;
    private TextView mTextView;
    private TextView mClockView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setAmbientEnabled();

        mContainerView = (BoxInsetLayout) findViewById(R.id.container);
        mTextView = (TextView) findViewById(R.id.text);
        mClockView = (TextView) findViewById(R.id.clock);

        try {
            List<ResolveInfo> resolveInfos = Utils.getWatchFaces(getApplicationContext());

            PackageManager pm = getPackageManager();
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);

            for (ResolveInfo resolveInfo : resolveInfos) {
                ApplicationInfo appInfo = pm.getApplicationInfo(resolveInfo.serviceInfo.packageName, 0);
                addAssetPath.invoke(assetManager, appInfo.publicSourceDir);
            }

            Resources oldRes = getResources();
            Resources newRes = new Resources(assetManager, oldRes.getDisplayMetrics(), oldRes.getConfiguration());

            String key = "com.google.android.wearable.watchface.preview";
            List<Drawable> previews = new ArrayList<>();
            for (ResolveInfo resolveInfo : resolveInfos) {
                int resId = resolveInfo.serviceInfo.metaData.getInt(key);
                if (resId != 0) {
                    Drawable d = newRes.getDrawable(resId);
                    previews.add(d);
                } else {
                    previews.add(null);
                }
            }

            Log.d("", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onEnterAmbient(Bundle ambientDetails) {
        super.onEnterAmbient(ambientDetails);
        updateDisplay();
    }

    @Override
    public void onUpdateAmbient() {
        super.onUpdateAmbient();
        updateDisplay();
    }

    @Override
    public void onExitAmbient() {
        updateDisplay();
        super.onExitAmbient();
    }

    private void updateDisplay() {
        if (isAmbient()) {
            mContainerView.setBackgroundColor(getResources().getColor(android.R.color.black));
            mTextView.setTextColor(getResources().getColor(android.R.color.white));
            mClockView.setVisibility(View.VISIBLE);

            mClockView.setText(AMBIENT_DATE_FORMAT.format(new Date()));
        } else {
            mContainerView.setBackground(null);
            mTextView.setTextColor(getResources().getColor(android.R.color.black));
            mClockView.setVisibility(View.GONE);
        }
    }
}
